<?php
/**
 * alltosun.com  CategoryController.class.php
 * ============================================================================
 * 版权所有 (C) 2014-2016 GoCMS内容管理系统
 * 官方网站:   http://www.gouguoyin.cn
 * 联系方式:   QQ:245629560
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 勾国印 (phper@gouguoyin.cn) $
 * $Date: 2015-3-11 下午4:00:17 $
 * $Id$
*/
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Common\Helper\Category;
class SystemListController extends AdminController {
    //栏目列表页
    public function index(){
        $category_list = M('Nav')->select();
        $category_list = Category::toLevel($category_list,'——');
        //dump($category_list);
        //die();
    	$this->assign('category_list', $category_list);
    	$this->display();
    }
    
    /**
     * start
     */
    ////添加栏目
    public function insert(){
    	$data['parent_id'] = I('parent_id',0);
    	$data['title']     = I('title',null);
    	$data['sort']      = I('sort',null);
    	$data['url']      = I('url',null);
    	$data['status']    = I('status',0);
    	$id = I('id',null);
    	$note = [
    			  'parent_id' => '父级分类',
    			  'title'     => '菜单名',
    			  'sort'      => '排序',
    			  'status'    => '状态',
    	          'url'       => '地址',
    			];
    	$bool = $this -> checkField($data,$note);
    	if($bool){
    	    if(empty($id)){
    	    	if( M('Nav')->where("url = '".$data['url']."'")->find() ){
    	    		$this -> error('地址不合法！#地址重复#');
    	    	}
    	        if ( M('Nav')->data($data)->add() ){
    	            $this -> success('添加成功');
    	        }
    	    }else{
    	        if( M('Nav')->where("id = '$id'")->save($data) ){
    	            $this -> success('更新成功');
    	        }
    	    }
    	}
    }
    ///删除栏目
    public function del(){
        $id = I('id',null);
        $where['id'] = $id;
        $note = [
                     'id' => '栏目编号',
                ];
        $bool = $this -> checkField($where, $note);
        if($bool){
           if( M("Nav")->where("parent_id = '$id'")->select() ){
               $this -> error('不能删除该栏目!#该栏目存在子栏目#');
           }
           if(M('Nav')->where($where)->delete()){
              $this -> success('删除成功'); 
            }
        }
    }
    
    ////更改
    public function update(){
        $id = I('id',null);
        $where['id'] = $id;
        $note = [
            'id' => '栏目编号',
        ];
        $bool = $this -> checkField($where, $note);
        if($bool){
            $res = M('Nav')->where($where)->find();
            $data['status'] = 1;
            $data['arr']  = $res;
            $this -> ajaxReturn($data);
        }else{
            $data['info'] = '数据不正确';
            $data['status'] = -1;
            $this -> ajaxReturn($data);
        }
    }
    
    /**
     * end
     */
    
    
    
    //增加 栏目页面
    public function add(){
        $parent_id = I('parent_id', 0);

        $parent_category_info = M('Category')->where(array('id' => $parent_id))->find();

        if($parent_category_info){
            $this->assign('parent_id', $parent_id);
            $this->assign('parent_category_info', $parent_category_info);
        }


        $model_list = M('Model')->where(array('status' => 1))->order('sort asc')->select();

        $category_list = M('category')->order('sort desc')->select();
        $category_list = Category::toLevel($category_list, '---',0);

        $this->assign('category_list', $category_list);
        $this->assign('action', 'add');
        $this->assign('model_list', $model_list);
        $this->display('category_add');
    }


    //修改栏目
    public function edit(){
        $category_id = I('id');
        $category_info = M('Category')->find($category_id);
        $model_list = M('Model')->order('sort desc')->select();

        $category_list = M('category')->order('sort desc')->select();
        $category_list = Category::toLevel($category_list, '---',0);

        $this->assign('category_list', $category_list);
        $this->assign('action', 'edit');
        $this->assign('category_id', $category_id);
        $this->assign('category_info', $category_info);
        $this->assign('model_list', $model_list);

        $this->display('category_add');
    }

    //保存栏目
    public function save(){
        $Category = D("Category");
        if (!$Category->create()){
            //验证没有通过 输出错误提示信息
            $errormsg = $Category->getError();
            $this->error($errormsg);
        }else{
            // 验证通过进行其他数据操作
            $id       = I('id', 0);

            $data['title']    = I('title', '');
            $data['flag']     = I('flag', '');
            $data['parent_id']     = I('parent_id', 0);
            $data['content']  = I('content', '');
            $data['sort']     = I('sort', 0);
            $data['status']   = I('status', 1);
            $data['is_show']  = I('is_show', 1);
            $data['has_cover']  = I('has_cover', 1);
            $data['cover']  = I('cover', '');

            $category_info = $Category->find($id);
            if($category_info){
            	//更新操作
                $data['update_time'] = time();

                $result = $Category->where(array('id' => $id))->save($data);
                if(false !== $result || 0 !== $result){
                    $this->success('栏目更新成功', U('Admin/Category/index'));
                }else{
                    $this->error('栏目更新失败');
                }
            }else{
                $data['model_id']   = I('model_id', 0);
                $data['model_flag'] = M('Model')->where(array('id' => I('model_id')))->getField('flag');
                $data['add_time']   = time();

                //print_r($data);exit;

                $result = $Category->add($data);
                if($result){
                    $this->success('栏目添加成功', U('Admin/Category/index'));
                }else{
                    $this->error('栏目添加失败');
                }
            }
        }
    }

    //更新状态
    public function change_status()
    {
        $id = I('id');

        $Category = M('Category');
        $category_info = $Category->find($id);
        if(!$category_info){
            $this->ajaxReturn(array('info' => '该栏目不存在'));
        }else{
            if($category_info['status']){
                $status = 0;
            }else{
                $status = 1;
            }
            $result = $Category->where(array('id' => $id))->setField('status',$status);
            if(false != $result){
                $this->ajaxReturn(array('status' => 'ok', 'info' => '栏目状态更新成功'));
            }else{
                $this->ajaxReturn(array('info' => '栏目状态更新失败'));
            }
        }

    }

    //保存排序
    public function save_sort()
    {
        $id   = I('id');
        $sort = I('sort');

        $Category = M('Category');
        $result = $Category->where(array('id' => $id))->setField('sort',$sort);
        if(false !== $result || 0 !== $result){
            $this->ajaxReturn(array('status' => 'ok', 'info' => '排序更新成功'));
        }else{
            $this->ajaxReturn(array('info' => '排序更新失败'));
        }
    }

    //删除栏目
    public function delete()
    {
        $id = I('id');

        $Category = M('Category');
        $category_info = $Category->find($id);
        if(!$category_info){
            return array('info' => '该模型不存在');
        }else if($category_info['is_system']){
            return array('info' => '系统模型不允许删除');
        }else{
            $result = $Category->delete($id);
            if($result){
                $this->ajaxReturn(array('status' => 'ok', 'info' => '栏目删除成功'));
            }else{
                $this->ajaxReturn(array('info' => '栏目删除失败'));
            }
        }
    }

}